/**
 * 
 */
package com.zt.samples.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * MyBatisDemo.
 * 
 * @author yinxb
 * 
 */
public class MyBatisDemo {
	/**
	 * 日志对象.
	 */
	static Logger LOG = Logger.getLogger("MyBatisDemo");

	/**
	 * 使用XML配置文件.
	 * 
	 * @param args
	 *            命令行参数
	 * @throws IOException
	 *             IO异常
	 */
	public static void main(String[] args) throws IOException {
		String resource = "com/zt/samples/mybatis/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
				.build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		try {
			session.update("com.zt.samples.mybatis.DemoBeanMapper.initDemo");
			// 只是用XML
			DemoBean dBean = (DemoBean) session.selectOne(
					"com.zt.samples.mybatis.DemoBeanMapper.selectDemo", 1L);
			LOG.info(dBean.toString());
			System.out.println(dBean.getAge());
		} finally {
			session.close();
		}
		// 不使用xml,只用mapper
		noXMLConfig();
	}

	/**
	 * 不使用XML配置文件配置MyBatis.
	 */
	public static void noXMLConfig() {
		Map<String,String> dsProp = new HashMap<String,String>();
		dsProp.put("driverClassName", "org.h2.Driver");
		dsProp.put("url", "jdbc:h2:DemoDB");
		dsProp.put("username", "sa");
		dsProp.put("password", "");
		dsProp.put("validationQuery", "select 1");
		dsProp.put("filters", "stat");
		DataSource dataSource = null;
		try {
			// 使用 alibaba DRUID连接池。
			dataSource = DruidDataSourceFactory.createDataSource(dsProp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("development",
				transactionFactory, dataSource);
		Configuration configuration = new Configuration(environment);
		// 注册mapper.
		configuration.addMapper(DemoNoXMLMapper.class);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
				.build(configuration);

		SqlSession session = sqlSessionFactory.openSession();
		try {
			// 不使用xml,只用mapper
			// 如果上面不注册mapper,再此按如下方法注册也可以。
			// sqlSessionFactory.getConfiguration().addMapper(DemoNoXMLMapper.class);
			DemoNoXMLMapper dm = session.getMapper(DemoNoXMLMapper.class);
			DemoBean demoBean = dm.selectDemo(1);
			if (demoBean != null) {
				LOG.info(demoBean.toString());
			} else {
				LOG.info("demoBean is NULL.");
			}
		} finally {
			session.close();
		}
	}
}
